package org.monkey4j;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.junit.runners.model.FrameworkField;
import org.junit.runners.model.TestClass;

/**
 * Utility class for handling some monkey related functionality.
 * 
 * @author hillger.t
 */
public class MonkeyUtil {
	private final TestClass testClass;

	public MonkeyUtil(Class<?> testClass) {
		this.testClass = new TestClass(testClass);
	}

	/**
	 * Returns a list of fields within a JUnit test which are marked with the {@link Monkey} annotation.
	 */
	public List<Field> getMonkeyAnnotatedFields() {
		List<Field> fields = new ArrayList<Field>();
		for (FrameworkField field : this.testClass.getAnnotatedFields(Monkey.class)) {
			field.getField().setAccessible(true);
			fields.add(field.getField());
		}
		return fields;
	}

	public String getMonkeyPropery(Field field) {
		Monkey monkey = (Monkey) field.getAnnotations()[0];
		return monkey.property();
	}
}
